package com.vhr.service.impl;

import com.vhr.VO.CheckCountVO;
import com.vhr.VO.DateVO;
import com.vhr.entities.*;
import com.vhr.mapper.CheckMapper;
import com.vhr.mapper.EmployeeMapper;
import com.vhr.mapper.LeaveMapper;
import com.vhr.mapper.OvertimeMapper;
import com.vhr.service.ICheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service

public class ICheckServiceImpl implements ICheckService {
    @Autowired
    public CheckMapper checkMapper;
    @Autowired
    public LeaveMapper leaveMapper;
    @Autowired
    public OvertimeMapper overtimeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    //查询出勤列表
    public List<Check> selectAllCheck(){
        CheckExample example = new CheckExample();
        List<Check> list = checkMapper.selectByExample(example);
        return list;
    }

    public List<CheckExtra> selectAllCheckWithName(){
        List<CheckExtra> list = checkMapper.selectAllWithName();
        return list;
    }

    //查询所有加班信息
    @Override
    public List<Overtime> selectAllOvertime() {
        OvertimeExample example = new OvertimeExample();
        List<Overtime> overtimeList = overtimeMapper.selectByExample(example);
        return overtimeList;
    }

    //查询所有请假信息
    @Override
    public List<Leave> selectAllLeave() {
        LeaveExample example = new LeaveExample();
        List<Leave> list = leaveMapper.selectByExample(example);
        return list;
    }

    //    管理员条件查询
    @Override
    //@Cacheable(cacheNames = "check",key = "'deptId_'+#deptId + 'empName_'+#empName + 'status_'+#status + 'yearDate_'+#yearDate + 'monthDate_'+#monthDate")
    @Cacheable(cacheNames = "check")
    public List<CheckExtra> selectCheckByCondition(Integer deptId, String empName, Integer status, String yearDate, String monthDate) {
        return checkMapper.selectCheckByCondition(deptId, empName, status, yearDate, monthDate);
    }

    //    管理员按日期查询
    @Override
    public List<CheckExtra> selectCheckByDate(String yearDate, String monthDate) {
        return checkMapper.selectCheckByDate(yearDate, monthDate);
    }

    //员工查询个人考勤信息
    @Override
    public List<Check> selectPersonCheck(Employee employee) {
        CheckExample example = new CheckExample();
        CheckExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(employee.getEmpId());
        List<Check> checkList = checkMapper.selectByExample(example);
        return checkList;
    }

    //员工查询个人考勤信息
    @Override
    @Cacheable(key = "#empolyee.empId")
    public List<CheckExtra> selectPersonCheckWithName(Integer empId) {
        List<CheckExtra> checkList = checkMapper.selectPersonCheckWithName(empId);
        return checkList;
    }

    //员工条件查询个人考勤信息
    @Override
    @Cacheable(key = "'empId_' + #empId + 'status_' + #status")
    public List<CheckExtra> selectPersonCheckByStatus(Integer empId, Integer status) {
        List<CheckExtra> checkList = checkMapper.selectPersonCheckWithNameByStatus(empId, status);
        return checkList;
    }

    //查询个人加班信息
    @Override
    @Cacheable(key = "'overtimePerson_' + #empId")
    public List<CheckExtra> selectPersonOvertime(Integer empId) {
        List<CheckExtra> checkList = checkMapper.selectPersonOvertime(empId);
        return checkList;
    }

    //查询个人请假信息
    @Override
    @Cacheable(key = "'leavePerson_' + #emp.empId")
    public List<Leave> selectPersonLeave(Employee emp) {
        LeaveExample example = new LeaveExample();
        LeaveExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(emp.getEmpId());
        List<Leave> leaveList = leaveMapper.selectByExample(example);
        return leaveList;
    }

//    插入请假考勤信息
    @Override
    public int insertCheck(Check check) {
        return checkMapper.insert(check);
    }

    //    统计出勤信息条数记录
    @Override
    public int selectCheckCount() {
        return checkMapper.checkCount();
    }

//    修改出勤情况
    @Override
    public int updateCheck(Check check) {
        return checkMapper.updateByCheck(check);
    }

    //统计出勤信息
    @Override
    public List<CheckCountVO> countByStatus(String date){
        //*是否正常出勤*//*

        //员工数
        List<DateVO> num = checkMapper.checkCountAll(date);

        List<CheckCountVO> checkCountVOList = new ArrayList<CheckCountVO>();

        List<DateVO> normalList = checkMapper.checkCountByStatus(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<num.size();i++){
            int normal = 0;
            int innormal = 0;
            int count = num.get(i).getNum();
            String countDate = sdf.format(num.get(i).getNowDate());
            System.out.println("riqi:"+countDate);
            if(normalList.size()>0){

                for(int j=0;j<normalList.size();j++){
                    String normalDate = sdf.format(normalList.get(j).getNowDate());
                    if(countDate.equals(normalDate)){
                        normal = normalList.get(j).getNum();
                        innormal = count-normal;
                        normalList.remove(j);
                        break;
                    }
                    innormal = count;
                }
            }else if(normalList.size()==0){
                innormal = count;
            }
            Calendar calendar = Calendar.getInstance();
            Integer days = 0;
            try{
                calendar.setTime(sdf.parse(countDate));
                days = calendar.get(Calendar.DAY_OF_MONTH);
            }catch (ParseException e){
                e.printStackTrace();
            }

            CheckCountVO checkCountVO = new CheckCountVO(days,normal,innormal);
            checkCountVOList.add(checkCountVO);
        }
        return checkCountVOList;

    }

    //计算加班工资:根据考勤表
    @Override
    public List<Check> countOverTimeByCheck(Integer empId, String yearMonth, Integer status) {
        return checkMapper.countOverTimeByCheck(empId,yearMonth,status);
    }

}
